/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Controller;

import BLL.ProductBLL;
import DAL.CategoryDAL;
import DAL.ProductDAL;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Iterator;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import utils.JAXBUtil;
import xmlobject.DTO.Categories.Category;
import xmlobject.DTO.Products.Product;
import xmlobject.DTO.Products.Products;

/**
 *
 * @author minhsong
 */
public class ProductController extends HttpServlet {

private ProductDAL service = new ProductDAL();
    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        
        //response.setContentType("text/html;charset=UTF-8");
        try {
            String action = request.getParameter("Action");
            if (action.equals("Category")) {
                response.setContentType("text/xml");
                String id = request.getParameter("Id");
                Products result = service.getProductsByCategoryId(Integer.parseInt(id));
                JAXBUtil.marshal(response.getOutputStream(), result);
            }else if(action.equals("Create")){

            }else if(action.equals("Edit")){
                int proId = Integer.parseInt(request.getParameter("ProductID"));
                String stockstr = request.getParameter("Stock");
                int stock  = stockstr.equals("")? 0:Integer.parseInt(stockstr);
                Product product = service.getProductById(proId);
                product.setStock(stock);
                service.Edit(product);
                (new ProductBLL()).ProductsToXML(getServletContext().getRealPath("/")+"xml/products.xml");
                RequestDispatcher rd = request.getRequestDispatcher("index.jsp");
                rd.forward(request, response);
            }else if(action.equals("Delete")){

            }else if(action.equals("Search")){
                response.setContentType("text/xml");
                String name = request.getParameter("name");
                Products result = service.Search(name);
                JAXBUtil.marshal(response.getOutputStream(), result);

            }else if(action.equals("Detail")){
                response.setContentType("text/xml");
                String id = request.getParameter("Id");
                int proId = Integer.parseInt(id);
                Product result = service.getProductById(proId);
                JAXBUtil.marshal(response.getOutputStream(), result);
            }
        } finally {
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
